﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import org.aswing.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.effects.*;
    import flash.filters.*;
    import org.aswing.geom.*;
    import Classes.doobers.*;
    import Classes.actions.*;
    import Classes.gates.*;
    import Display.aswingui.*;
    import Display.PopulationUI.*;
    import Engine.*;
    import Modules.stats.types.*;
    import com.zynga.skelly.util.*;
    import Modules.quest.Helpers.*;
    import Modules.franchise.transactions.*;

    public class ConstructionSite extends MapResource {

        public static const BUILD_GATE:String = "build";
        public static const PRE_BUILD_GATE:String = "pre_build";
        public static const STATE_IDLE:int = 0;
        public static const STATE_BUILDING:int = 1;
        public static const STATE_AT_GATE:int = 2;
        public static const STATE_CAN_BE_FINISHED:int = 3;
        public static const STATE_FINISHED:int = 4;
        public static const STATE_PRE_GATE:int = 5;
        public static const IS_QUEUE_ALL_BUILDS:Boolean = false;

        private const CONSTRUCTION_SITE:String = "construction_site";
        private const ALL_GATE_IDS:Array;

        private var m_currentState:int = 5;
        private var m_displayStage:Array;
        private var m_displayStageNPCOffset:Array;
        private var m_stage:int = 0;
        private var m_numberOfStages:int = 0;
        private var m_buildsPerStage:int;
        private var m_energyPerBuild:int;
        private var m_energyStart:int;
        private var m_timePerBuild:int;
        private var m_buildStartTime:Number;
        private var m_builds:int = 0;
        private var m_finishedBuilds:int = 0;
        private var m_totalNumberOfBuilds:int;
        private var m_targetBuildingName:String;
        private var m_targetBuildingFriendlyName:String;
        private var m_targetBuildingClass:Class;
        private var m_buildQueue:Array;
        private var m_gates:Dictionary;
        private var m_gateFactory:GateFactory;
        private var m_isAccelerating:Boolean = false;
        private var m_accelerateStartTime:Number = 0;
        private var m_buildTimeLeftAtAccelStart:Number = 0;
        private var m_isAccelerated:Boolean = false;
        private var m_accelerationEnergyCost:Number;
        private var m_scaffoldEffect:ScaffoldEffect;
        private var m_numBoosts:int;
        private var m_visitingId:String;
        protected var m_mouseOverMessage:String = null;

        public function ConstructionSite(_arg1:String){
            var _local2:LotSite;
            var _local3:String;
            var _local5:*;
            var _local6:Vector2;
            this.ALL_GATE_IDS = new Array(BUILD_GATE, PRE_BUILD_GATE);
            this.m_displayStage = new Array();
            this.m_displayStageNPCOffset = new Array();
            this.m_buildQueue = new Array();
            this.m_gates = new Dictionary();
            super(_arg1);
            m_objectType = WorldObjectTypes.CONSTRUCTION_SITE;
            m_typeName = this.CONSTRUCTION_SITE;
            var _local4:XMLList = m_item.rawImageXml;
            for (_local5 in _local4) {
                if (_local4[_local5].@type == "displayStage"){
                    if (_local3 != _local4[_local5].@name){
                        this.m_displayStage.push(_local4[_local5].@name);
                        _local6 = new Vector2(0, 0);
                        if (_local4[_local5].@npcOffsetX){
                            _local6.x = int(_local4[_local5].@npcOffsetX);
                        };
                        if (_local4[_local5].@npcOffsetY){
                            _local6.y = int(_local4[_local5].@npcOffsetY);
                        };
                        this.m_displayStageNPCOffset.push(_local6);
                        this.m_numberOfStages++;
                        _local3 = _local4[_local5].@name;
                    };
                };
            };
            this.m_numberOfStages--;
            setState(this.m_displayStage[this.m_stage]);
            this.m_buildsPerStage = m_item.xml.buildsPerStage;
            this.m_energyStart = m_item.xml.energyStart;
            this.m_energyPerBuild = m_item.xml.energyCostPerBuild;
            this.m_timePerBuild = m_item.xml.timePerBuild;
            if (RuntimeVariableManager.getBoolean("IS_TEST_ENVIRONMENT", false)){
                this.m_timePerBuild = (m_item.xml.timePerBuild / 10);
            } else {
                this.m_timePerBuild = m_item.xml.timePerBuild;
            };
            this.m_totalNumberOfBuilds = (this.m_buildsPerStage * this.m_numberOfStages);
            this.m_numBoosts = 0;
            this.m_gateFactory = new GateFactory();
            this.m_gateFactory.register(GateType.INVENTORY, InventoryGate);
            this.m_gateFactory.register(GateType.CREW, CrewGate);
            if (this.m_numberOfStages != int(m_item.xml.numberOfStages)){
                ErrorManager.addError((("Error processing " + _arg1) + ": unexpected number of build stages!"));
            };
        }
        public static function createConstructionSite(_arg1:String, _arg2:Class=null):ConstructionSite{
            var _local3:Item = Global.gameSettings.getItemByName(_arg1);
            _arg2 = ((_arg2) || (ItemClassDefinitions.getClassByItem(_local3)));
            var _local4:ConstructionSite = new ConstructionSite(_local3.construction);
            _local4.initializeSite(_local3, _arg2);
            _local4.itemOwner = Global.player.uid;
            _local4.setOuter(Global.world);
            return (_local4);
        }

        override protected function statsInit():void{
            super.statsInit();
            m_statsName = ItemClassDefinitions.getTypeByClass(this.m_targetBuildingClass);
            m_statsItemName = this.m_targetBuildingName;
        }
        protected function getPreviewImageName(_arg1:Item):String{
            switch (_arg1.type){
                case "landmark":
                case "municipal":
                case "business":
                    return ("static");
                default:
                    return ("grown");
            };
        }
        override protected function getCurrentImageForMask():ItemImageInstance{
            var _local1:Item;
            var _local2:String;
            if (this.m_currentState == STATE_AT_GATE){
                _local1 = this.targetItem;
                _local2 = this.getPreviewImageName(_local1);
                return (_local1.getCachedImage(_local2, this, m_direction, m_statePhase));
            };
            return (super.getCurrentImageForMask());
        }
        override public function isPixelInside(_arg1:Point):Boolean{
            if ((((this.m_currentState == STATE_AT_GATE)) && ((m_content is Bitmap)))){
                m_maskBitmap = (content as Bitmap);
            };
            return (super.isPixelInside(_arg1));
        }
        public function get targetName():String{
            return (this.m_targetBuildingName);
        }
        public function get currentState():int{
            return (this.m_currentState);
        }
        public function get targetClass():Class{
            return (this.m_targetBuildingClass);
        }
        public function get targetItem():Item{
            return (Global.gameSettings.getItemByName(this.m_targetBuildingName));
        }
        public function get timePerBuild():Number{
            var _local1:Number = (Global.gameSettings.getInt("constructionVisitBoost") * 3600000);
            var _local2:Number = (this.m_totalNumberOfBuilds * m_item.xml.timePerBuild);
            var _local3:Number = (1 - ((this.m_numBoosts * _local1) / _local2));
            return ((m_item.xml.timePerBuild * _local3));
        }
        public function initializeSite(_arg1:Item, _arg2:Class):void{
            var _local3:String;
            var _local4:Function;
            this.m_targetBuildingName = _arg1.name;
            this.m_targetBuildingClass = _arg2;
            this.m_targetBuildingFriendlyName = this.targetItem.localizedName;
            for each (_local3 in this.ALL_GATE_IDS) {
                _local4 = this.getGateCompleteCallback(_local3);
                this.m_gates[_local3] = this.m_gateFactory.loadGateFromXML(_arg1, this, _local3, _local4);
            };
            this.setMouseOverMessage(_arg1);
            this.statsInit();
        }
        override public function getItemFriendlyName():String{
            var _local1:Item = Global.gameSettings.getItemByName(this.targetName);
            var _local2:String = ((_local1.className == "Headquarter")) ? Global.franchiseManager.getHeadquartersFriendlyName(_local1.name) : _local1.localizedName;
            return (_local2);
        }
        override public function loadObject(_arg1:Object):void{
            var _local3:int;
            super.loadObject(_arg1);
            this.m_builds = _arg1.builds;
            this.m_finishedBuilds = _arg1.finishedBuilds;
            this.m_currentState = _arg1.currentState;
            this.m_buildStartTime = _arg1.buildStartTime;
            this.m_targetBuildingName = _arg1.targetBuildingName;
            this.m_targetBuildingClass = (getDefinitionByName(("Classes." + _arg1.targetBuildingClass)) as Class);
            this.m_targetBuildingFriendlyName = this.targetItem.localizedName;
            this.m_isAccelerated = _arg1.isAccelerated;
            this.m_numBoosts = _arg1.numBoosts;
            var _local2:Item = this.targetItem;
            this.loadGatesFromObject(_arg1, _local2);
            if (this.m_isAccelerated){
                this.m_builds = (this.m_finishedBuilds = this.m_totalNumberOfBuilds);
                this.m_stage = (this.m_numberOfStages - 1);
                if (_arg1.gates){
                    this.m_currentState = STATE_AT_GATE;
                };
            } else {
                this.m_stage = _arg1.stage;
            };
            setState(this.m_displayStage[this.m_stage]);
            this.setDisplayObjectDirty(true);
            if (this.m_currentState == STATE_BUILDING){
                _local3 = 0;
                while (_local3 < (_arg1.buildsQueued.length - 1)) {
                    this.m_buildQueue.push(true);
                    _local3++;
                };
                if (this.m_buildQueue.length){
                    _local3 = 0;
                    while (_local3 < (this.m_buildQueue.length - 1)) {
                        this.createNPCs();
                        _local3++;
                    };
                };
            };
            this.statsInit();
            this.setMouseOverMessage(_local2);
        }
        protected function loadGatesFromObject(_arg1:Object, _arg2:Item):void{
            var _local5:Object;
            var _local6:Function;
            var _local3:IGate;
            var _local4:Array = _arg1.gates;
            for each (_local5 in _local4) {
                if ((((_local5.name == null)) || ((_local5.name == "")))){
                    _local5.name = BUILD_GATE;
                };
                _local6 = this.getGateCompleteCallback(_local5.name);
                _local3 = this.m_gateFactory.loadGateFromXML(_arg2, this, _local5.name, _local6);
                if (((_local3) && ((_local3.loadType == AbstractGate.LOAD_TYPE_DYNAMIC)))){
                    this.m_gates[_local5.name] = _local3;
                } else {
                    this.m_gates[_local5.name] = this.m_gateFactory.loadGateFromObject(_arg1, _arg2, this, _local6);
                };
            };
        }
        protected function getGateCompleteCallback(_arg1:String):Function{
            var _local2:Function;
            switch (_arg1){
                case BUILD_GATE:
                    _local2 = this.completeBuilding;
                    break;
                case PRE_BUILD_GATE:
                    _local2 = this.completePreGate;
                    break;
            };
            return (_local2);
        }
        override protected function onStateChanged(_arg1:String, _arg2:String):void{
            this.updateArrow();
            this.updateScaffold();
            if (this.m_currentState == STATE_IDLE){
                Global.world.citySim.npcManager.removeAllConstructionWorkers(this);
            };
        }
        override public function drawDisplayObject():void{
            super.drawDisplayObject();
            this.updateScaffold();
        }
        private function updateScaffold():void{
            if (!this.m_targetBuildingName){
                return;
            };
            if (this.m_item.showGirders == 0){
                return;
            };
            var _local1:int = ((((!(this.m_isAccelerating)) && (this.m_buildQueue.length))) ? 1 : 0 + (((this.m_isAccelerating) && (!((this.m_currentState == STATE_AT_GATE))))) ? 2 : 0);
            var _local2:int = (this.m_finishedBuilds + _local1);
            var _local3:int = (this.m_finishedBuilds + _local1);
            var _local4:int = this.m_numberOfStages;
            var _local5:String = this.m_targetBuildingName;
            if (this.m_scaffoldEffect){
                this.m_scaffoldEffect.startStage = _local2;
                this.m_scaffoldEffect.endStage = _local3;
                this.m_scaffoldEffect.maxStage = _local4;
                this.m_scaffoldEffect.targetBuildingName = _local5;
                this.m_scaffoldEffect.reattach();
            } else {
                this.m_scaffoldEffect = new ScaffoldEffect(this, _local2, _local3, _local4, _local5);
            };
        }
        protected function doPlayAction():void{
            var _local1:Boolean;
            switch (this.m_currentState){
                case STATE_PRE_GATE:
                    if (Global.isVisiting()){
                        return;
                    };
                    if (((this.m_gates[PRE_BUILD_GATE]) && ((this.m_gates[PRE_BUILD_GATE].keyCount > 0)))){
                        this.m_gates[PRE_BUILD_GATE].displayGate(null, ZLoc.t("Dialogs", "PreGateDialogTitle", {item:this.m_targetBuildingFriendlyName}));
                        break;
                    };
                    this.completePreGate();
                    this.doConstruction();
                    break;
                case STATE_IDLE:
                    this.doConstruction();
                    break;
                case STATE_BUILDING:
                    _local1 = true;
                    this.doConstruction(_local1);
                    break;
                case STATE_AT_GATE:
                    if (Global.isVisiting()){
                        return;
                    };
                    if (((this.m_gates[BUILD_GATE]) && ((this.m_gates[BUILD_GATE].keyCount > 0)))){
                        this.m_gates[BUILD_GATE].displayGate();
                        break;
                    };
                    this.completeBuilding();
                    break;
            };
            m_actionMode = NO_ACTION;
        }
        override public function onPlayAction():void{
            var _local1:Boolean = Boolean(((m_actionBar) && (m_actionBar.visible)));
            var _local2:Boolean = Boolean((m_visitReplayLock > 0));
            if (((_local1) || (_local2))){
                return;
            };
            m_actionMode = PLAY_ACTION;
            this.doPlayAction();
        }
        override public function onVisitPlayAction():void{
            m_actionMode = VISIT_PLAY_ACTION;
            this.doVisitAction();
            m_actionMode = NO_ACTION;
            trackVisitAction(TrackedActionType.HELP_BUILD);
        }
        override public function onVisitReplayAction(_arg1:TRedeemVisitorHelpAction):Number{
            m_actionMode = VISIT_REPLAY_ACTION;
            GameTransactionManager.addTransaction(_arg1);
            this.doVisitAction();
            var _local2:Number = super.onVisitReplayAction(null);
            return (_local2);
        }
        private function doVisitAction():void{
            switch (this.m_currentState){
                case STATE_IDLE:
                    this.doConstruction();
                    break;
                case STATE_BUILDING:
                    this.visitBoost();
                    break;
                case STATE_AT_GATE:
                    break;
            };
        }
        override public function updateAlpha():void{
            if (((((m_actionBar) && (m_actionBar.visible))) || (this.m_isAccelerating))){
                this.alpha = 1;
            } else {
                super.updateAlpha();
            };
        }
        override protected function updateActionBarPosition():void{
            var _local1:Vector2;
            var _local2:Point;
            if (((m_actionBar) && (m_actionBar.visible))){
                _local1 = this.getWorkerOffsetForCurrentStage();
                _local2 = IsoMath.tilePosToPixelPos((m_position.x + _local1.x), (m_position.y + _local1.y));
                _local2.y = _local2.y;
                _local2 = IsoMath.viewportToStage(_local2);
                m_actionBar.x = (_local2.x - (ACTIONBAR_WIDTH / 2));
                m_actionBar.y = ((_local2.y - ACTIONBAR_HEIGHT) - 60);
            };
        }
        override public function getConstructionNPCOffset():Point{
            return (new Point(this.getWorkerOffsetForCurrentStage().x, this.getWorkerOffsetForCurrentStage().y));
        }
        override public function isVisitorInteractable():Boolean{
            switch (this.m_currentState){
                case STATE_IDLE:
                    return (true);
                case STATE_BUILDING:
                    return (true);
                case STATE_AT_GATE:
                    return (false);
            };
            return (false);
        }
        override public function onObjectDrag(_arg1:Vector3):void{
            Global.world.citySim.npcManager.removeAllConstructionWorkers(this);
            super.onObjectDrag(_arg1);
        }
        override public function onObjectDrop(_arg1:Vector3):void{
            var _local2:int;
            if (this.m_buildQueue.length){
                _local2 = 0;
                while (_local2 < (this.m_buildQueue.length - 1)) {
                    this.createNPCs();
                    _local2++;
                };
            };
            if (this.m_scaffoldEffect){
                this.m_scaffoldEffect.cleanUp();
                this.m_scaffoldEffect = null;
                this.updateScaffold();
            };
            super.onObjectDrop(_arg1);
        }
        override public function isSellable():Boolean{
            return (true);
        }
        override public function getToolTipHeader():String{
            var _local1:String;
            var _local2:String;
            var _local3:String;
            if (((m_actionBar) && (m_actionBar.visible))){
                return (null);
            };
            if (this.m_targetBuildingName.slice(0, 2) == "hq"){
                _local1 = Global.franchiseManager.getFranchiseTypeFromHeadquarters(this.m_targetBuildingName);
                _local2 = Global.franchiseManager.worldOwnerModel.getFranchiseName(_local1);
                _local3 = ZLoc.t("Items", "headquarters", {item:_local2});
                return (_local3);
            };
            return (this.m_targetBuildingFriendlyName);
        }
        override public function setId(_arg1:Number):void{
            super.setId(_arg1);
            if (this.m_gates[BUILD_GATE]){
                this.m_gates[BUILD_GATE].targetObjectId = getId();
            };
        }
        override public function getToolTipStatus():String{
            var _local2:int;
            var _local3:String;
            var _local4:String;
            if (((((m_actionBar) && (m_actionBar.visible))) && (!(this.m_isAccelerating)))){
                return (null);
            };
            var _local1:String;
            if (!IS_QUEUE_ALL_BUILDS){
                if (((((((((((!(isLocked())) && (!(Global.isVisiting())))) && (!(Global.world.isEditMode)))) && (!((this.m_currentState == STATE_BUILDING))))) && (!((this.m_currentState == STATE_FINISHED))))) && (!((this.m_currentState == STATE_PRE_GATE))))){
                    _local1 = ZLoc.t("Main", "BuildInfo", {
                        current:this.m_builds,
                        required:(this.m_numberOfStages * this.m_buildsPerStage)
                    });
                };
            } else {
                if (this.m_currentState == STATE_BUILDING){
                    if (this.m_isAccelerating){
                        _local2 = this.getAcceleratedBuildTimeLeft();
                        if (_local2 >= 0){
                            _local3 = GameUtil.formatMinutesSeconds(_local2);
                            _local1 = ZLoc.t("Main", "BuildingAccelerating");
                        };
                    } else {
                        _local4 = GameUtil.formatMinutesSeconds(this.getQueuedBuildTimeLeft());
                        _local1 = ZLoc.t("Main", "BuildingReadyIn", {time:_local4});
                    };
                };
            };
            return (_local1);
        }
        public function forceUpdateArrow():void{
            this.updateArrow();
        }
        override protected function updateArrow():void{
            this.createStagePickEffect();
        }
        override protected function createStagePickEffect():void{
            var _local1:String;
            if (((!(Global.guide.isActive())) && (!(Global.isVisiting())))){
                if ((((((this.m_stage >= this.m_numberOfStages)) && (!((this.m_gates[BUILD_GATE] == null))))) && (this.m_gates[BUILD_GATE].checkForKeys()))){
                    _local1 = StagePickEffect.PICK_UPGRADE_HAMMER;
                };
            };
            if (_local1){
                m_stagePickEffect = (MapResourceEffectFactory.createEffect(this, EffectType.STAGE_PICK) as StagePickEffect);
                m_stagePickEffect.setPickType(_local1);
                m_stagePickEffect.float();
            } else {
                removeStagePickEffect();
            };
        }
        public function getQueuedBuildTimeLeft():int{
            var _local1:Number = GlobalEngine.getTimer();
            var _local2:int = (this.m_totalNumberOfBuilds - this.m_finishedBuilds);
            var _local3:int = (this.timePerBuild * _local2);
            var _local4:int = (((this.m_buildStartTime + _local3) - _local1) / 1000);
            if (_local4 < 0){
                _local4 = 0;
            };
            return (_local4);
        }
        public function getAcceleratedBuildTimeLeft():int{
            var _local1:Number = GlobalEngine.getTimer();
            var _local2:Number = (this.m_accelerateStartTime + this.m_timePerBuild);
            var _local3:int = ((((_local2 - _local1) / (_local2 - this.m_accelerateStartTime)) * this.m_buildTimeLeftAtAccelStart) / 1000);
            return (_local3);
        }
        protected function getBuildCost():Number{
            var _local1:Number;
            var _local2:int;
            var _local3:int;
            if (((Global.isVisiting()) || ((m_actionMode == VISIT_REPLAY_ACTION)))){
                _local1 = 0;
            } else {
                if (((this.m_isAccelerating) && (this.m_accelerationEnergyCost))){
                    _local1 = this.m_accelerationEnergyCost;
                } else {
                    if (this.m_builds <= 0){
                        _local1 = this.m_energyStart;
                    } else {
                        if (!IS_QUEUE_ALL_BUILDS){
                            _local1 = this.m_energyPerBuild;
                        } else {
                            _local2 = ((this.m_finishedBuilds)>0) ? this.m_finishedBuilds : 1;
                            _local3 = (this.m_totalNumberOfBuilds - _local2);
                            _local1 = (_local3 * this.m_energyPerBuild);
                        };
                    };
                };
            };
            return (_local1);
        }
        override public function getToolTipAction():String{
            var _local2:Number;
            var _local3:Boolean;
            if (((m_actionBar) && (m_actionBar.visible))){
                return (null);
            };
            var _local1:String = getGameModeToolTipAction();
            if (!_local1){
                if (((((((((!(isLocked())) && (!(Global.isVisiting())))) && (!(Global.world.isEditMode)))) && (((IS_QUEUE_ALL_BUILDS) || (!((this.m_currentState == STATE_BUILDING))))))) && (!((this.m_currentState == STATE_FINISHED))))){
                    _local2 = this.getBuildCost();
                    switch (this.m_currentState){
                        case STATE_IDLE:
                        case STATE_BUILDING:
                        case STATE_PRE_GATE:
                            if (((IS_QUEUE_ALL_BUILDS) && ((this.m_builds > 0)))){
                                if (_local2 >= 0){
                                    _local3 = Global.player.checkEnergy(-(_local2), false);
                                    if (_local3){
                                        if (!this.m_isAccelerating){
                                            _local1 = ZLoc.t("Main", "InstantFinishNoEnergy", {energy:_local2});
                                        };
                                    } else {
                                        _local1 = ZLoc.t("Main", "NeedEnergy", {amount:_local2});
                                    };
                                } else {
                                    _local1 = ZLoc.t("Main", "ClickToFinish");
                                };
                            } else {
                                if (Global.player.checkEnergy(-(_local2), false)){
                                    _local1 = ZLoc.t("Main", "ClickToBuild");
                                } else {
                                    _local1 = ZLoc.t("Main", "NeedEnergy", {amount:_local2});
                                };
                            };
                            break;
                        case STATE_AT_GATE:
                            if (Global.player.checkEnergy(-(_local2), false)){
                                _local1 = ZLoc.t("Main", "ClickToFinish");
                            } else {
                                _local1 = ZLoc.t("Main", "NeedEnergy", {amount:_local2});
                            };
                            break;
                    };
                };
                if (Global.isVisiting()){
                    _local2 = this.getBuildCost();
                    switch (this.m_currentState){
                        case STATE_IDLE:
                        case STATE_BUILDING:
                            if (((IS_QUEUE_ALL_BUILDS) && ((this.m_builds > 0)))){
                                if (_local2 >= 0){
                                    _local3 = Global.player.checkEnergy(-(_local2), false);
                                    if (_local3){
                                        _local1 = ZLoc.t("Main", "ClickToBoost", {energy:_local2});
                                    } else {
                                        _local1 = ZLoc.t("Main", "NeedEnergy", {amount:_local2});
                                    };
                                };
                            } else {
                                if (Global.player.checkEnergy(-(_local2), false)){
                                    _local1 = ZLoc.t("Main", "ClickToBuild");
                                } else {
                                    _local1 = ZLoc.t("Main", "NeedEnergy", {amount:_local2});
                                };
                            };
                            break;
                    };
                };
            };
            return (_local1);
        }
        private function doConstruction(_arg1:Boolean=false):void{
            var _local6:int;
            var _local7:int;
            var _local8:int;
            var _local2 = 1;
            var _local3:int = (this.m_totalNumberOfBuilds - this.m_builds);
            if (IS_QUEUE_ALL_BUILDS){
                _local2 = _local3;
            };
            var _local4:Number = this.getBuildCost();
            var _local5:Boolean;
            if (Global.isVisiting()){
                _local5 = !(Global.player.isVisitorEnergyZero());
            } else {
                if (m_actionMode == VISIT_REPLAY_ACTION){
                    _local5 = true;
                } else {
                    _local5 = Global.player.checkEnergy(-(_local4), false);
                };
                if (this.inPopulationGate(!((m_actionMode == VISIT_REPLAY_ACTION)))){
                    _local5 = false;
                };
            };
            if (this.m_builds >= this.m_totalNumberOfBuilds){
                if (((((((((_arg1) && (IS_QUEUE_ALL_BUILDS))) && (!(this.m_isAccelerating)))) && (!(this.m_isAccelerated)))) && (_local5))){
                    this.startAcceleration();
                } else {
                    displayStatus(ZLoc.t("Main", "UnderConstruction"));
                };
            } else {
                if (Global.isVisiting()){
                    m_numVisitorInteractions++;
                };
                if (_local5){
                    _local6 = _local2;
                    if (!((_arg1) && ((_local2 > 0)))){
                        this.buildSite();
                        _local6--;
                    };
                    if (_local6 > 0){
                        while (_local6--) {
                            this.m_buildQueue.push(true);
                        };
                    };
                    this.createNPCs();
                    this.updateScaffold();
                    if (_local2 > 0){
                        this.m_builds = (this.m_builds + _local2);
                    };
                    Sounds.playFromSet(Sounds.SET_BUILDING_CONSTRUCTION);
                    if (m_actionMode != VISIT_REPLAY_ACTION){
                        if (Global.isVisiting()){
                            _local7 = Global.gameSettings.getInt("friendVisitConstructionRepGain", 1);
                            _local8 = Global.gameSettings.getNumber("friendHelpDefaultCoinReward", 10);
                            finalizeAndAwardVisitorHelp(VisitorHelpType.CONSTRUCTIONSITE_CONSTRUCT, _local7, _local8);
                            if (this.m_builds == this.m_totalNumberOfBuilds){
                                NeighborVisitManager.setVisitFlag(NeighborVisitManager.VISIT_FINISHEDBUILDING);
                            };
                        } else {
                            doEnergyChanges(-(_local4), this.getEnergyTrackingInfo(this.targetItem));
                            Global.player.heldEnergy = (Global.player.heldEnergy + _local4);
                            this.m_visitingId = Global.getVisiting();
                        };
                    };
                    m_actionQueue.removeAllStates();
                    m_actionQueue.addActions(new ActionProgressBar(null, this, ZLoc.t("Main", "Building"), (this.m_timePerBuild / 1000)));
                } else {
                    if (!Global.player.checkEnergy(-(_local4))){
                        displayStatus(ZLoc.t("Main", "NotEnoughEnergyRed"));
                    };
                };
            };
        }
        private function getEnergyTrackingInfo(_arg1:Item):Array{
            var _local2:String = _arg1.name;
            var _local3:Array = new Array("energy", "expenditures", "build_unknown", _local2);
            if (_local2.indexOf("res_") >= 0){
                _local3[2] = "build_house";
            } else {
                if (_local2.indexOf("mun_") >= 0){
                    _local3[2] = "build_municipal";
                } else {
                    if (_local2.indexOf("bus_") >= 0){
                        _local3[2] = "build_business";
                    } else {
                        if (_local2.indexOf("storage_") >= 0){
                            _local3[2] = "build_storage";
                        } else {
                            if (_local2.indexOf("goods_pier") >= 0){
                                _local3[2] = "build_pier";
                            } else {
                                _local3[2] = "other";
                            };
                        };
                    };
                };
            };
            return (_local3);
        }
        private function visitBoost():void{
            var _local1:int;
            var _local2:int;
            this.m_numBoosts++;
            this.buildSite();
            this.updateScaffold();
            if (Global.isVisiting()){
                _local1 = Global.gameSettings.getInt("friendVisitConstructionRepGain", 1);
                _local2 = Global.gameSettings.getNumber("friendHelpDefaultCoinReward", 10);
                finalizeAndAwardVisitorHelp(VisitorHelpType.CONSTRUCTIONSITE_ACCEL, _local1, _local2, 0);
                if (this.m_builds == this.m_totalNumberOfBuilds){
                    NeighborVisitManager.setVisitFlag(NeighborVisitManager.VISIT_FINISHEDBUILDING);
                };
            };
            m_numVisitorInteractions++;
            m_actionQueue.removeAllStates();
            m_actionQueue.addActions(new ActionProgressBar(null, this, ZLoc.t("Main", "Boosting")));
        }
        private function startAcceleration():void{
            this.m_isAccelerating = true;
            this.m_accelerateStartTime = GlobalEngine.getTimer();
            var _local1:int = (this.m_totalNumberOfBuilds - this.m_finishedBuilds);
            var _local2:int = (this.timePerBuild * _local1);
            this.m_buildTimeLeftAtAccelStart = ((this.m_buildStartTime + _local2) - this.m_accelerateStartTime);
            this.m_accelerationEnergyCost = this.getBuildCost();
            this.m_timePerBuild = 2000;
            var _local3:int = (this.m_totalNumberOfBuilds - this.m_builds);
            this.enqueueBuilds(_local3);
            m_actionQueue.removeAllStates();
            Sounds.play("speedup");
            Sounds.playFromSet(Sounds.SET_BUILDING_CONSTRUCTION);
            m_actionQueue.addActions(new ActionProgressBar(null, this, ZLoc.t("Main", "Finishing"), (this.m_timePerBuild / 1000)));
        }
        private function enqueueBuilds(_arg1:int):void{
            var _local2:int = _arg1;
            if (_local2 > 0){
                while (_local2--) {
                    this.m_buildQueue.push(true);
                };
            };
            if (_arg1 > 0){
                this.m_builds = (this.m_builds + _arg1);
            };
            this.buildSite();
            this.updateScaffold();
        }
        private function setToGatedStage():void{
            this.m_builds = (this.m_finishedBuilds = this.m_totalNumberOfBuilds);
            this.m_stage = (this.m_numberOfStages - 1);
            this.m_currentState = STATE_AT_GATE;
            setState(this.m_displayStage[this.m_stage]);
        }
        private function buildSite():void{
            this.m_currentState = STATE_BUILDING;
        }
        private function checkBuildQueue():Boolean{
            if (this.m_buildQueue.length >= this.m_totalNumberOfBuilds){
                return (false);
            };
            var _local1:int = (this.m_builds + 1);
            if (_local1 >= this.m_totalNumberOfBuilds){
                return (false);
            };
            return (true);
        }
        protected function getWorkerOffsetForCurrentStage():Vector2{
            var _local1:Vector2 = new Vector2(0, 0);
            if (this.m_stage <= this.m_numberOfStages){
                if (this.m_displayStageNPCOffset[this.m_stage]){
                    _local1 = (this.m_displayStageNPCOffset[this.m_stage] as Vector2);
                };
            };
            return (_local1);
        }
        protected function createNPCs():void{
            var _local2:Object;
            var _local1:Array = [];
            _local2 = {};
            _local2.pos = new Vector3(0, 0);
            _local2.isTempWorker = false;
            _local1[_local1.length] = _local2;
            Global.world.citySim.npcManager.addConstructionWorkers(this, _local1);
        }
        private function onBuildComplete():void{
            this.m_currentState = STATE_IDLE;
            this.m_finishedBuilds++;
            if (Global.world.getObjectById(getId()) == null){
                return;
            };
            this.updateBuildingProgress();
            if (this.m_buildQueue.length > 0){
                this.m_buildQueue.pop();
                this.buildSite();
                return;
            };
        }
        override protected function makeDoobers(_arg1:Number=1):Array{
            var _local4:Array;
            var _local2:Array = Global.player.processRandomModifiers(this.targetItem, this, true, m_secureRands);
            var _local3:Array = new Array();
            for each (_local4 in _local2) {
                if (_local4[0] == Global.gameSettings.getDooberFromType(Doober.DOOBER_XP, _local4[1])){
                    _local3.push(_local4);
                };
            };
            return (_local3);
        }
        public function completeBuilding():void{
            var _local3:ConstructionSite;
            var _local4:Array;
            if (Global.isTransitioningWorld){
                return;
            };
            if (Global.isVisiting()){
                return;
            };
            if (this.inPopulationGate(!((m_actionMode == VISIT_REPLAY_ACTION)))){
                return;
            };
            Global.world.citySim.npcManager.removeAllConstructionWorkers(this);
            m_doobersArray = Global.player.processRandomModifiers(m_item, this, true, m_secureRands);
            spawnDoobers();
            var _local1:MapResource = new this.m_targetBuildingClass(this.m_targetBuildingName);
            this.m_currentState = STATE_FINISHED;
            trackAction(TrackedActionType.CONSTRUCTION_COMPLETE);
            this.detach();
            this.cleanUp();
            _local1.setOuter(Global.world);
            _local1.setPosition(this.getPosition().x, this.getPosition().y);
            _local1.setId(this.getId());
            _local1.itemOwner = this.itemOwner;
            _local1.attach();
            _local1.setActive(true);
            _local1.onBuildingConstructionCompleted_PreServerUpdate();
            _local1.startTime = m_startTime;
            _local1.rotateToDirection(this.getDirection());
            Global.world.citySim.recomputePotentialPopulation(Global.world);
            Global.world.citySim.roadManager.updateRoads(_local1);
            if (!Global.isVisiting()){
                Global.player.commodities.updateCapacities();
                Global.hud.updateCommodities();
                _local4 = Global.world.wildernessSim.processExpansionsOnConstructionComplete(this);
                GameTransactionManager.addTransaction(new TConstructionFinish(this, _local4));
            } else {
                GameTransactionManager.addTransaction(new TFranchiseRefresh());
            };
            var _local2:Array = Global.world.getObjectsByClass(ConstructionSite);
            for each (_local3 in _local2) {
                _local3.forceUpdateArrow();
            };
            _local1.onBuildingConstructionCompleted_PostServerUpdate();
        }
        override public function getCursor():Class{
            var _local1:Class = super.getCursor();
            if (((!((this.m_currentState == STATE_FINISHED))) && ((this.m_builds < this.m_totalNumberOfBuilds)))){
                _local1 = EmbeddedArt.hud_act_construction;
            } else {
                if (IS_QUEUE_ALL_BUILDS){
                    _local1 = EmbeddedArt.hud_act_zap;
                };
            };
            return (_local1);
        }
        override public function getCustomCursor():Object{
            var _local2:ASFont;
            var _local3:GlowFilter;
            var _local4:MarginBackground;
            if (!m_customCursor){
                m_customCursor = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
                m_customCursorLabel = new JLabel("");
                m_customCursorHolder = new Sprite();
                Global.ui.addChild(m_customCursorHolder);
                m_customCursorWindow = new JWindow(m_customCursorHolder);
                _local2 = ASwingHelper.getBoldFont(16);
                _local3 = new GlowFilter(0, 1, 1.2, 1.2, 20, BitmapFilterQuality.HIGH);
                m_customCursorLabel.setForeground(new ASColor(0xFFFFFF));
                m_customCursorLabel.setFont(_local2);
                m_customCursorLabel.setTextFilters([_local3]);
            };
            var _local1:DisplayObject;
            if (((!((this.m_currentState == STATE_FINISHED))) && ((this.m_builds < this.m_totalNumberOfBuilds)))){
                _local1 = (new EmbeddedArt.hud_act_construction() as DisplayObject);
                m_customCursorLabel.setText(this.getBuildCost().toString());
            } else {
                return (null);
            };
            if (_local1){
                _local4 = new MarginBackground(_local1, new Insets(0, 0, 0, 0));
                m_customCursor.setBackgroundDecorator(_local4);
                m_customCursor.setPreferredSize(new IntDimension(_local1.width, _local1.height));
                m_customCursor.setMinimumSize(new IntDimension(_local1.width, _local1.height));
                m_customCursor.setMaximumSize(new IntDimension(_local1.width, _local1.height));
            };
            m_customCursor.append(m_customCursorLabel);
            m_customCursorWindow.setContentPane(m_customCursor);
            ASwingHelper.prepare(m_customCursorWindow);
            m_customCursorWindow.show();
            return (m_customCursorWindow);
        }
        private function updateBuildingProgress():void{
            if (((((this.m_finishedBuilds % this.m_buildsPerStage) == 0)) && (!((this.m_finishedBuilds == 0))))){
                this.m_stage++;
                setState(this.m_displayStage[this.m_stage]);
                setDisplayObjectDirty(true);
                conditionallyRedraw(true);
                if (this.m_stage >= this.m_numberOfStages){
                    if (((this.m_gates[BUILD_GATE]) && ((this.m_gates[BUILD_GATE].keyCount > 0)))){
                        this.m_currentState = STATE_AT_GATE;
                        if (this.m_isAccelerating){
                            GameTransactionManager.addTransaction(new TConstructionFinish(this));
                            this.setToGatedStage();
                            this.updateScaffold();
                            this.m_isAccelerating = false;
                        };
                    } else {
                        this.completeBuilding();
                    };
                };
            };
        }
        override public function getProgressBarEndFunction():Function{
            return (Curry.curry(function (_arg1:ConstructionSite):void{
                var _local2:*;
                if (!Global.isVisiting()){
                    if (_arg1.m_visitingId != Global.getVisiting()){
                        return;
                    };
                    if (Global.world.getObjectById(_arg1.getId()) == null){
                        return;
                    };
                    if (m_actionMode != VISIT_REPLAY_ACTION){
                        GameTransactionManager.addTransaction(new TConstructionBuild(_arg1));
                    };
                    _local2 = _arg1.getBuildCost();
                    Global.player.heldEnergy = (Global.player.heldEnergy - _local2);
                    _arg1.trackAction(TrackedActionType.BUILD);
                    if (_arg1.m_builds != _arg1.m_totalNumberOfBuilds){
                        _arg1.m_doobersArray = _arg1.makeDoobers();
                        _arg1.spawnDoobers();
                    };
                };
                _arg1.onBuildComplete();
            }, this));
        }
        public function getGate(_arg1:String):IGate{
            return (this.m_gates[_arg1]);
        }
        protected function inPopulationGate(_arg1:Boolean):Boolean{
            var _local4:Object;
            var _local5:int;
            var _local2:Item = Global.gameSettings.getItemByName(this.targetName);
            var _local3:int = _local2.populationBase;
            if (_local3){
                if ((Global.world.citySim.getPopulation() + _local3) > Global.world.citySim.getPopulationCap()){
                    if (_arg1){
                        _local4 = {capNeeded:Global.world.citySim.getRequiredNonTotalPopulationCap(_local2)};
                        ResourceExplanationDialog.show(ResourceExplanationDialog.TYPE_NOT_ENOUGH_HAPPINESS_RESIDENCE, false, null, _local4);
                    };
                    return (true);
                };
            } else {
                if (this.targetClass == Business){
                    _local5 = Math.floor((Global.world.citySim.getPopulationCap() * Global.gameSettings.getNumber("businessLimitByPopulationMax")));
                    if (Global.world.citySim.getTotalBusinesses() > _local5){
                        if (_arg1){
                            _local4 = {capNeeded:Global.world.citySim.getRequiredPopulationCap(_local2)};
                            ResourceExplanationDialog.show(ResourceExplanationDialog.TYPE_NOT_ENOUGH_HAPPINESS_BUSINESS, false, null, _local4);
                        };
                        return (true);
                    };
                };
            };
            return (false);
        }
        override public function cleanUp():void{
            if (this.m_scaffoldEffect){
                this.m_scaffoldEffect.cleanUp();
            };
            if (this.m_gateFactory){
                this.m_gateFactory.cleanUp();
            };
            this.m_gateFactory = null;
            Global.world.citySim.npcManager.removeAllConstructionWorkers(this);
            super.cleanUp();
        }
        override public function sell():void{
            Global.world.citySim.npcManager.removeAllConstructionWorkers(this);
            super.sell();
        }
        override public function get sellSendsToInventory():Boolean{
            var _local1:Item = this.targetItem;
            if (_local1){
                return (_local1.sellSendsToInventory);
            };
            return (false);
        }
        override public function sendToInventory():void{
            var _local2:Boolean;
            var _local1:Item = this.targetItem;
            if (_local1){
                _local2 = Global.player.inventory.addItems(this.targetName, 1);
                if (_local2){
                    Global.world.citySim.npcManager.removeAllConstructionWorkers(this);
                    GameTransactionManager.addTransaction(new TSendToInventory(this));
                    detach();
                    cleanup();
                    this.onSell();
                };
            };
        }
        override public function onBuildingConstructionCompleted_PreServerUpdate():void{
            Global.world.citySim.recomputePotentialPopulation(Global.world);
        }
        override public function onSell():void{
            super.onSell();
            Global.world.citySim.recomputePotentialPopulation(Global.world);
        }
        override protected function getSellPrice():int{
            return (this.targetItem.cost);
        }
        override public function canBeDragged():Boolean{
            var _local1 = !((((this.targetClass == Bridge)) || ((this.targetClass == BridgePart))));
            return (((_local1) && (!(((this.m_isAccelerating) || ((this.m_currentState == STATE_BUILDING)))))));
        }
        override public function canBeRotated():Boolean{
            var _local1 = !((((this.targetClass == Bridge)) || ((this.targetClass == BridgePart))));
            return (((_local1) && (super.canBeRotated())));
        }
        override public function get isNeedingRoad():Boolean{
            var _local1:Boolean;
            if ((((((this.targetClass == Business)) || ((this.targetClass == Residence)))) || ((this.targetClass == Factory)))){
                _local1 = !(isAdjacentToAnyRoad);
            } else {
                _local1 = false;
            };
            return (_local1);
        }
        public function get isAccelerating():Boolean{
            return (this.m_isAccelerating);
        }
        override public function getActionTargetName():String{
            return (this.m_targetBuildingName);
        }
        override public function getVisitReplayEquivalentActionString():String{
            return ("build");
        }
        override public function checkPlacementRequirements(_arg1:int, _arg2:int):Boolean{
            var _local3:MapResource = (new this.m_targetBuildingClass(this.m_targetBuildingName) as MapResource);
            return (_local3.checkPlacementRequirements(_arg1, _arg2));
        }
        private function completePreGate():void{
            this.m_currentState = STATE_IDLE;
        }
        override public function onMouseOver(_arg1:MouseEvent):void{
            super.onMouseOver(_arg1);
            if (((!((this.m_mouseOverMessage == null))) && (!((this.m_mouseOverMessage == ""))))){
                Global.ui.showTickerMessage(this.m_mouseOverMessage, false);
            };
        }
        protected function setMouseOverMessage(_arg1:Item):void{
            var _local2:XML = _arg1.constructionXml;
            var _local3 = "";
            if (((_local2) && ((_local2.attribute("mouseOverText").length() > 0)))){
                _local3 = _local2.attribute("mouseOverText")[0];
            };
            if (_local3 != ""){
                this.m_mouseOverMessage = ZLoc.t("Main", _local3);
            };
        }

    }
}//package Classes 
